﻿using MiniFox.Caching;
using MiniFox.Components;
using MiniFox.Platform.Applications.Configurations;
using MiniFox.Platform.Applications.Models;
using MiniFox.Platform.Applications.Providers;
using MiniFox.Platform.Configurations;
using MiniFox.Web;

namespace MiniFox.Platform.Applications
{
    /// <summary>
    /// 系统平台(在集成passport环境中为SSO客户端应用程序环境)，主要维护应用程序菜单和当前配置
    /// </summary>
    public class PlatformService : Component, IPlatformService
    {
        /// <summary>
        /// 模块列表缓存键名
        /// </summary>
        const string SYS_MODULE = @"APP_SYS_MODULE";

        internal const string ENV_CONFIG_KEY = @"ENV_CONFIG";

        #region 构造

        /// <summary>
        /// 
        /// </summary>
        public PlatformService()
        {
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        [AutoWired]
        protected IPlatformProvider PlatformProvider
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        [AutoWired]
        protected IRoleProvider RoleProvider
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        [AutoConfigure]
        protected virtual DesktopConfiguration DesktopConfiguration 
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        [AutoConfigure]
        protected virtual EnvironmentConfiguration EnvironmentConfiguration
        {
            get;
            set;
        }
        #region 

        /// <summary>
        /// 
        /// </summary>
        public virtual IEnumerable<SystemNavModuleEntry> SystemModules
        {
            get
            {
                return CacheService.GetCacheObject(SYS_MODULE, PlatformProvider.GetSystemModules, 3600);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="moduleId"></param>
        /// <returns></returns>
        public SystemNavModuleEntry GetSystemModule(string moduleId)
        {
            var module = this.SystemModules.SingleOrDefault(m => moduleId == m.ID);
            return module;
        }
        #endregion

        /// <summary>
        /// 获取默认全局环境配置
        /// </summary>
        public virtual SystemGlobalConfig GetSystemGlobalConfig()
        {
            return CacheService.GetCacheObject(ENV_CONFIG_KEY, () =>
            {
                var config = this.PlatformProvider.GetEnvironmentConfig();
                if (config != null)
                {
                    return config;
                }
                return new SystemGlobalConfig
                {
                    LogoUri = this.EnvironmentConfiguration.LogoUri,
                    IconUri = this.EnvironmentConfiguration.IconUri,
                    Title = this.EnvironmentConfiguration.Title,
                    Description = this.EnvironmentConfiguration.Description,
                    CopyRight = this.EnvironmentConfiguration.CopyRight,
                    ICP = this.EnvironmentConfiguration.ICP,
                    DefaultApplicationID = this.DesktopConfiguration.DefaultApplicationID,
                    HomeUri = this.DesktopConfiguration.HomeUri,
                    Theme = this.DesktopConfiguration.Theme,
                    WarningUri = this.DesktopConfiguration.WarningUri,
                    BuiltinUrls = this.DesktopConfiguration.BuiltinUrls,
                };

            });
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fullUrl"></param>
        /// <returns></returns>
        public SystemNavModuleEntry GetSystemModule(Uri fullUrl)
        {
            string url = fullUrl.ClearQueryString().ToString();
            var module = this.SystemModules.SingleOrDefault(m => string.Equals(m.Url, url, StringComparison.OrdinalIgnoreCase));
            return module;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public string[] GetRoles(SystemNavModuleEntry module)
        {
            var roles = this.RoleProvider.GetModuleRoles(module);
            var roleName = roles.Select(r => r.Name);
            return roleName.ToArray();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public string[] GetRoles(SystemNavModuleAction action)
        {
            var roles = this.RoleProvider.GetModuleRoles(action);
            var roleName = roles.Select(r => r.Name);
            return roleName.ToArray();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fullUrl"></param>
        /// <returns></returns>
        public string[] GetRoles(string fullUrl)
        {
            Uri uri = new Uri(fullUrl);
            var moduleEntry = this.GetSystemModule(uri);
            if (moduleEntry == null)
                return new string[0];
            return this.GetRoles(moduleEntry);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fullUrl"></param>
        /// <param name="entryUrl"></param>
        /// <returns></returns>
        public string[] GetRoles(string fullUrl, string entryUrl)
        {
            if (string.IsNullOrWhiteSpace(entryUrl))
            {
                return this.GetRoles(fullUrl);
            }
            Uri uri = new Uri(entryUrl);
            var moduleEntry = this.GetSystemModule(uri);
            if (moduleEntry == null)
                return new string[0];
            var moduleAction = moduleEntry.Actions.SingleOrDefault(a => string.Equals(a.Url, fullUrl, StringComparison.OrdinalIgnoreCase));
            return this.GetRoles(moduleAction);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="identity"></param>
        /// <returns></returns>
        public IEnumerable<SystemRole> GetUserRoles(SystemIdentity identity)
        {
            return this.RoleProvider.GetUserRoles(identity);
        }
    }
}
